﻿using System.Collections.Generic;
using ShapeConnectors.MaTrix;

namespace ShapeConnectors
{
    class TPLT:MaTran
    {
        public static List<BuocThucHien> Steptplt = new List<BuocThucHien>();
        public static List<CANHNA> canhlt = new List<CANHNA>();
        string chuoi = null;

        bool flag = false;

        //private int d = 0;

        List<int> nhanlt = new List<int>();
        public void ViSit(int i, int lb)
        {
            
            nhanlt[i]=lb;
            for (int j = 0; j < spt; j++)
            {
                
                if (nhanlt[j] == 0 && (a[i, j] > 0))
                {

                    flag = true;
                    BuocThucHien buoc = new BuocThucHien();
                    buoc.KhoiTao();
                    buoc.DinhChon = i;
                    buoc.Dinhcanto = j;
                    buoc.Canhcanto.SetDinh1(i);
                    buoc.Canhcanto.SetDinh2(j);
                    buoc.Canhcanto.SetTS(a[i,j]);
                    buoc.Label = lb;
                    Steptplt.Add(buoc);

                    //d = j;

                    chuoi = chuoi + " , " + j.ToString();
                    ViSit(j, lb);
                   
                }
                

            }

            
        }
        public string FindConnected()
        {
            int lb=0;
            for (int i = 0; i < spt; i++)
                nhanlt.Add(0);
            for (int i = 0; i < spt; i++)
            {
               
                if (nhanlt[i] == 0)
                {
                    if (chuoi != null)
                    {
                        chuoi = chuoi + "\r\n";
                    }
                    lb++;
                    chuoi = chuoi + "Thành phần liên thông thứ " + lb.ToString() + " là: " + i.ToString();

                    ViSit(i, lb);
                    if (flag == false)
                    {
                        BuocThucHien buoc2 = new BuocThucHien();
                        buoc2.KhoiTao();
                        buoc2.DinhChon = i;
                        buoc2.Dinhcanto = -1;
                        buoc2.Canhcanto.SetDinh1(i);
                        buoc2.Canhcanto.SetDinh2(-1);
                        buoc2.Canhcanto.SetTS(-1);
                        buoc2.Label = lb;
                        Steptplt.Add(buoc2);
                    }
                    else
                        flag = false;
                }
            }
            return chuoi;
        }
        
    }
}
